home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: Pointer to read file...
- Date: 19 Apr 1996 22:28 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <19APR199622282058@erich.triumf.ca>
- References: <1996Apr19.110223@bbs.ug.eds.com>
- NNTP-Posting-Host: ftp.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <1996Apr19.110223@bbs.ug.eds.com>, berryb@bbs.ug.eds.com (High Plains GRIPster (TRW C.S.D.)) writes...
- >4/19/96
- >
- >I have a beginners C question.
- >After going page by page through my Learning C book,
- >I am still in a quandry.
- >
- >I have a pointer to a file: FILE *fp;
- >
- >I have a pointer to a pointer: FILE **p2f = &fp;
- >
- >I have a character pointer: char*fname="/user2/bcb/grip/unix/c/listing";
- >
- >After I open my file... if ( (fp = fopen (file1, "r")) != NULL)
-
- I hope you really mean
- if((fp = fopen(fname, "r")) != NULL)
- here?? (otherwise, what is "file1"?)
-
- >
- >I want to be able to use the p2f pointer, which is pointing at
- >the beginning of the file, to read the character that it is
- >pointing at...and then move to the next character and read
- >that character until EOF is reached.
-
- a FILE *, in spite of it's name, does not point _at_ or _into_ the file. It
- points to some system specific structure describing the file, and where or how
- to find it's contents - we don't need to know what's in this structure, but the
- file access functions provided with the compiler do know.
-
- >
- >I am able to increment the pointers position by p2f++;
- >but I am unable to read the character. This is probably
- >because I need a character pointer to read a character,
- >and the FILE pointer just cant perform that way.
-
- correct. You don't need p2f at all.
-
- You can read a line from the file thus:
-
- char buffer[BIG_ENUF];
- fgets(buffer, BIG_ENUF, fp);
-
- or you can use fgetc() or getc() to get a single char.
-
- After fgets() gets a line (or as much will fit in the buffer) or gets() or
- fgets() get a char, a "pointer into the file" is positioned ready to read the
- next char, or bunch of chars. If you are just reading or writing a file
- sequentially, you don't need to worry about this "pointer into the file" - it
- will be automagically incremented as needed by the file access functions.
-
- You can position this "pointer into the file" using fseek(), ftell(), and
- related functions - consult your compiler's function library reference.
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
- or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
- or: http:://vancouver-webpages.com/peter/index.html
-
-
-
-
-
-
-